package com.put.mybills;

import android.content.Context;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveFolder.DriveFolderResult;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;
import com.put.mybills.cloud.InterfaceEvent;

public class CreateDirectory {
	
	private static final String TAG = "CreateDirectory";
	
	private GoogleApiClient mGoogleApiClient;
	private Context context;
	
	private InterfaceEvent event;
	private String dirName;
	private String dirParent;
	
	public CreateDirectory(GoogleApiClient mGoogleApiClient, Context context)
	{		
		this.mGoogleApiClient = mGoogleApiClient;
		this.context = context;
	}
	
    public void createFolder(String dirName, String dirParent) {  
    	
    	Log.d(TAG,"Creating folder name : " + dirName);
		
    	this.dirName= dirName;
    	this.dirParent = dirParent;
		
    	if (mGoogleApiClient.isConnected()) {
    		
    		if (dirParent.equals("")) {    			
    			MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle(dirName).build();        
        		Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(mGoogleApiClient, changeSet).setResultCallback(folderCreatedCallback);
    		}
    		else {    
    			//DriveFolder folder = Drive.DriveApi.getAppFolder(mGoogleApiClient);
    			//Log.d(TAG, "APP FOLDER = " + folder.getDriveId().encodeToString());
    			DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, DriveId.decodeFromString(dirParent));
	            MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle(dirName).build();
	            folder.createFolder(mGoogleApiClient, changeSet).setResultCallback(createFolderCallback);    			
    			//Drive.DriveApi.fetchDriveId(mGoogleApiClient, DriveId.decodeFromString(dirParent)).setResultCallback(idCallback);
    		}	
    	}
    	else {
    		Log.d(TAG,"Client google drive not connected");		
    	
    	}
    }

	public InterfaceEvent getEvent() {
		return event;
	}

	public void setEvent(InterfaceEvent event) {
		this.event = event;
	}
	
	
	final ResultCallback<DriveFolderResult> folderCreatedCallback = new
            ResultCallback<DriveFolderResult>() {

		@Override
		public void onResult(DriveFolderResult result) {					
			  if (!result.getStatus().isSuccess()) {
				  	Log.d(TAG,"Error while trying to create the folder " + result.getStatus());		               
				  	event.bad_reaction();
	          }	
			  else { 				  
				  event.good_reaction(result.getDriveFolder().getDriveId().encodeToString());			
			  }
			}
	};	


    final ResultCallback<DriveFolderResult> createFolderCallback = new
	            ResultCallback<DriveFolderResult>() {

	        @Override
	        public void onResult(DriveFolderResult result) {
	            if (!result.getStatus().isSuccess()) {
	            	event.bad_reaction("Problem while trying to create a folder");	               
	            }
	            event.good_reaction(result.getDriveFolder().getDriveId().encodeToString());
	        }
	    };

}
